/**
 * MicroStrategy SDK Sample
 *
 * Copyright ? 2011 MicroStrategy Incorporated. All Rights Reserved.
 *
 * MICROSTRATEGY MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE 
 * SUITABILITY OF THIS SAMPLE CODE, EITHER EXPRESS OR IMPLIED, INCLUDING 
 * BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS 
 * FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT. MICROSTRATEGY SHALL NOT 
 * BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, 
 * MODIFYING OR DISTRIBUTING THIS SAMPLE CODE OR ITS DERIVATIVES.
 *
 *
 */

package com.amex.uam.task;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.amex.uam.action.CountryRegionAction;
import com.amex.uam.action.MstrAction;
import com.amex.uam.action.ProjAction;
import com.amex.uam.action.UserAction;
import com.amex.uam.model.Message;
import com.amex.uam.util.Utils;
import com.microstrategy.web.beans.MarkupOutput;
import com.microstrategy.web.beans.RequestKeys;
import com.microstrategy.web.objects.WebIServerSession;
import com.microstrategy.web.tasks.AbstractBaseTask;
import com.microstrategy.web.tasks.TaskException;
import com.microstrategy.web.tasks.TaskParameterMetadata;
import com.microstrategy.web.tasks.TaskRequestContext;

public class SearchUserTask extends AbstractBaseTask {

	public static final String SID_SEARCH_USER = "searchUser";
	public static final String SID_ACTIVATE_USER = "ActivateUser";
	public static final String SID_REMOVE_USER = "RemoveUser";
	public static final String SID_LOAD_USER_PROFILE = "LoadUserProfile";
	public static final String SID_LOAD_ALL_PROJECTS = "LoadAllProjects";
	public static final String SID_LOAD_SECURITY_GROUP_BY_GROUP_NAME = "LoadSecurityGroupByGroupName";
	public static final String SID_LOAD_USER_PROJ = "LoadUserProj";
	public static final String SID_LOAD_USER_SECURITY_GROUP = "LoadUserSecurityGroup";
	public static final String SID_LOAD_GEO_CTRY = "LoadGEOCounrty";
	public static final String SID_LOAD_GEO_RGN = "LoadGEORegion";
	public static final String SID_LOAD_GEO_CTRY_BY_AMEXWEBID = "LoadGEOCTRYByAmexWebId";
	public static final String SID_LOAD_GEO_RGN_BY_AMEXWEBID = "LoadGEORGNByAmexWebId";
	public static final String SID_VUP_UPDATE_USER_GEN_INFO = "UpdateVUPUSRGenInfo";
	//public static final String SID_VUP_UPDATE_MSTR_PROD = "UpdateVUPMSTRProd";
	public static final String SID_CREATE_SHARED_REPORT_GROUP = "CreateSharedReportGroup";

	/*private static BeanFactory factory;
	static {
		try {
			factory = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
			System.out.println("test hello world");
		} catch (Throwable e) {
			e.printStackTrace();
		}
	}*/
	//ApplicationContext ctxApplicationContext = WebApplicationContextUtils.getWebApplicationContext(sc);
	
	private TaskParameterMetadata taskSubID;
	public SearchUserTask() {
		super("SearchUserTask");
		taskSubID = addParameterMetadata("TaskSubID", "Enter Operation", true, "");
	}

	@Override
	public void processRequest(TaskRequestContext context, MarkupOutput markupOutput) throws TaskException {
		
		//ContainerServices cs = context.getContainerServices();
		//HttpServletRequest request = ((ServletContainerServices)cs).getRequest();
		//ServletContext sc = request.getSession().getServletContext();
		//ApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(sc);
		
		BeanFactory ctx = new ClassPathXmlApplicationContext("beans.xml");
		
		RequestKeys keys = context.getRequestKeys();
		String subId = keys.getValue(taskSubID.getName());

		// System.out.println("Sub ID: " + taskSub + ", session-id: " + keys.getValue("session"));

		WebIServerSession session = null;
		try {
			session = context.getWebIServerSession(null, "session");
		} catch (Throwable t) {
		}

		Message message = null;

		System.out.println("** SearchUserTask->taskSub: " + subId);
		
		if (SID_SEARCH_USER.equalsIgnoreCase(subId)) {
			message = ctx.getBean("userAction", UserAction.class).searchUser(keys, session);
			//UserAction ua = factory.getBean("userAction", UserAction.class);
		} else if(SID_ACTIVATE_USER.equalsIgnoreCase(subId)){
			//load the user status enable/disable then color star.
			message = ctx.getBean("mstrAction", MstrAction.class).activateUser(keys, session);
		} else if(SID_REMOVE_USER.equalsIgnoreCase(subId)){
			message = ctx.getBean("userAction", UserAction.class).removeUser(keys, session);
		} else if (SID_LOAD_USER_PROFILE.equalsIgnoreCase(subId)) {
			message = ctx.getBean("userAction", UserAction.class).loadUserProfile(keys);
		} else if (SID_LOAD_ALL_PROJECTS.equalsIgnoreCase(subId)) {
			message = ctx.getBean("projAction", ProjAction.class).loadAllProjects();
		} else if (SID_LOAD_SECURITY_GROUP_BY_GROUP_NAME.equalsIgnoreCase(subId)) {
			message = ctx.getBean("mstrAction", MstrAction.class).loadSecurityGroupByGroupName(keys, session);
		} else if (SID_LOAD_USER_PROJ.equalsIgnoreCase(subId)) {
			message = ctx.getBean("projAction", ProjAction.class).loadUserProjects(keys);
		} else if (SID_LOAD_USER_SECURITY_GROUP.equalsIgnoreCase(subId)) {
			message = ctx.getBean("userAction", UserAction.class).loadUserSecurityGroup(keys);
		}else if(SID_LOAD_GEO_CTRY.equalsIgnoreCase(subId)){
			message = ctx.getBean("countryRegionAction", CountryRegionAction.class).loadGEOCounrty();
		}else if(SID_LOAD_GEO_RGN.equalsIgnoreCase(subId)){
			message = ctx.getBean("countryRegionAction", CountryRegionAction.class).loadGEORegion();
		}else if(SID_LOAD_GEO_CTRY_BY_AMEXWEBID.equalsIgnoreCase(subId)){
			message = ctx.getBean("userAction", UserAction.class).loadGEOCTRYByAmexWebId(keys);
		}else if(SID_LOAD_GEO_RGN_BY_AMEXWEBID.equalsIgnoreCase(subId)){
			message = ctx.getBean("userAction", UserAction.class).loadGEORGNByAmexWebId(keys);
		}else if(SID_CREATE_SHARED_REPORT_GROUP.equalsIgnoreCase(subId)) {
			message = ctx.getBean("mstrAction", MstrAction.class).createSharedReportGroup(keys, session);
		}else if(SID_VUP_UPDATE_USER_GEN_INFO.equalsIgnoreCase(subId)){
			UserAction userAction = ctx.getBean("userAction", UserAction.class);
			MstrAction mstrAction = ctx.getBean("mstrAction", MstrAction.class);
			
			//2. User Type
			//a. update the value in MS_USER_SEC_GRP table's SEC_GRP_NM column
			userAction.updateUserType(keys);
			//b. change User to specified security group
			mstrAction.updateUserType(keys, session);
			
			//3. MicroStrategy Product Select
			//a. update values in table MS_USER_ADMIN join MS_PROJ
			userAction.updateVUPMSTRProd(keys);

			//4. User Profile General Information
			//update MS_USER_ADMIN table
			userAction.updateVUPUSRGenInfo(keys);
			mstrAction.setAccessEndDate(keys, session);
			//5. GMAPS
			//a. change MS_USER_SEC_GRP value based on Radio button's value
			//b. update/create/delete user security based on Radio button's value
			userAction.updateGMAPS(keys);
			mstrAction.updateGMAPS(keys, session);
			
			//6. MARC Functional Group
			//a. update MS_USER_FUNC_GRP based on checkbox's value
			//b. update MD based on checkbox's value
			userAction.updateMarcFuncGroup(keys, session);

			//7. Opt Blue
			//a. change MS_USER_OPT_BLUE_ACS_RSN table's OPT_BLUE_ACS_RSN_CD
			//b. update Security Group based on user's Radio button's value
			userAction.updateOptBlue(keys);
			
			mstrAction.updateOptBlue(keys, session);
			
			//System.out.println(msg.toJSON());
			//8. Shared Reports
			//a. update the Security Groups in MD based on Admin's operations on shared folders.
			mstrAction.updateSharedReports(keys, session);
			userAction.updateRGNCTRY(keys);
			
			userAction.notifyApproved(keys);
			
			message = new Message(Message.OK);
		}else {
			message = new Message(Message.ERROR_SUBID, "Invalid TaskSubID: " + subId);
		} 
		
		if (message != null) {
			//System.out.println(message.toJSON());
			markupOutput.append(message.toJSON());
		} else {
			System.out.println("Nothing respond!");
			markupOutput.append("");
		}
	}

}